package com.xinbochuang.machine.admin.service.impl.macRoom;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.macRoom.EquShow;
import com.xinbochuang.machine.admin.domain.macRoom.Equipment;
import com.xinbochuang.machine.admin.domain.macRoom.EquipmentDetail;
import com.xinbochuang.machine.admin.domain.macRoom.json.Node;
import com.xinbochuang.machine.admin.mapper.macRoom.EquShowMapper;
import com.xinbochuang.machine.admin.service.macRoom.IEquShowService;
import com.xinbochuang.machine.admin.service.macRoom.IEquipmentDetailService;
import com.xinbochuang.machine.admin.service.macRoom.IEquipmentService;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄晓鹏
 * @date 2020-12-12
 */
@Service
public class EquShowServiceImpl extends ServiceImpl<EquShowMapper, EquShow> implements IEquShowService {

    @Resource
    private EquShowMapper equShowMapper;

    @Resource
    private IEquipmentDetailService detailService;

    @Resource
    private IEquipmentService equipmentService;

    /**
     * 保存设备节点信息
     *
     * @param nodes 节点信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveNodes(List<Node> nodes) {
        List<Equipment> equipmentList = new ArrayList<>();
        List<EquShow> list = new ArrayList<>();
        List<EquipmentDetail> detailList = new ArrayList<>();
        String now = DateUtil.now();
        String createBy = SecurityUtils.getUsername();
        for (Node node : nodes) {
            EquShow equShow = new EquShow();
            if (node.getId() != null) {
                equShow.setId(node.getId());
            }
            equShow.setCabinetId(node.getCabinetId());
            equShow.setNode(JSONUtil.toJsonStr(node));
            list.add(equShow);

            EquipmentDetail detail = node.getEqu();
            if (StrUtil.isBlank(detail.getUuid())) {
                detail.setUuid(IdUtil.simpleUUID());
            }
            detail.setCabinetId(node.getCabinetId());
            detail.setCreateTime(now);
            detail.setCreateBy(createBy);
            detailList.add(detail);

            Equipment equ = new Equipment();
            if (StrUtil.isBlank(node.getEquId())) {
                equ.setUuid(IdUtil.simpleUUID());
            } else {
                equ.setUuid(node.getEquId());
            }
            equ.setCabinetId(node.getCabinetId());
            equ.setEquName(detail.getEquName());
            equ.setEquState("上电");
            equ.setEquHeight(detail.getUfloorNum());
            equ.setCreateTime(now);
            equ.setCreateBy(createBy);
            if (StrUtil.isNotBlank(detail.getUfloorNum()) && detail.getUfloorNum().contains("-")) {
                try {
                    String[] nums = detail.getUfloorNum().split("-");
                    int end = Integer.parseInt(nums[1]);
                    int begin = Integer.parseInt(nums[0]);
                    equ.setHeight(end - begin + 1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            equipmentList.add(equ);
        }
        // 保存设备
        equipmentService.saveOrUpdateBatch(equipmentList);
        for (int i = 0; i < equipmentList.size(); i++) {
            list.get(i).setEquId(equipmentList.get(i).getUuid());
            detailList.get(i).setEquId(equipmentList.get(i).getUuid());
        }
        // 保存节点数据
        saveOrUpdateBatch(list);
        // 保存设备详情
        detailService.saveOrUpdateBatch(detailList);
        return true;
    }
}
